package yxy.game.pm2.module.guard;

import yxy.apple.logger.Logger;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.guardreward;
import yxy.cherry.data.string.GameString;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.rank.AreaRankModule;
import yxy.game.pm2.bean.module.rank.PlayerRankModule;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.module.mail.MailHandle;
import yxy.game.pm2.module.mail.MailTypeEnum;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

public class RankGuard extends PlayerRankModule {
    private static final Logger logger = Logger.create(AreaRankModule.class);
    public int guardIndex;

    public RankGuard(AreaBean areaBean, int guardIndex) {
        super(areaBean);
        this.guardIndex = guardIndex;
        initInfo();
    }

    @Override
    public String rankName() {
        return PlayerRankModule.getAllRankNames().get(3);
    }

    @Override
    protected long valuePlastic(long value) {
        return value * 100;
    }

    @Override
    protected long valueRestore(long score) {
        return score / 100;
    }

    @Override
    protected String rankKey() {
        return String.format("AREA:%s:RANK:%s:%s", getAreaId(), rankName(), guardIndex);
    }

    static public RankGuard getRank(String playerCode, int guardIndex) {
        AreaBean areaBean = AreaModule.area(playerCode);
        return new RankGuard(areaBean, guardIndex);
    }

    static public RankGuard getRank(AreaBean areaBean, int guardIndex) {
        return new RankGuard(areaBean, guardIndex);
    }

    public void reward() {
        new Runnable() {
            @Override
            public void run() {
                ArrayList<RankEntry> dailyTop = rankTop();
                rewardRank(dailyTop);
            }
        }.run();
    }

    private ArrayList<RankEntry> rankTop() {
        int min = -1;// 全局最小
        int max = -1;// 全局最大
        Map<String, guardreward> all = DataCenter.getAllData(guardreward.class);
        for (guardreward data : all.values()) {
            if (min < 0 || data.getRankingmin() < min) {
                min = data.getRankingmin();
            }
            if (max < 0 || data.getRankingmax() > max) {
                max = data.getRankingmax();
            }
        }
        return new ArrayList<>(range(min, max));
    }

    private void rewardRank(ArrayList<RankEntry> rankTop) {
        logger.info("服务器%s::洛阳守卫发奖励了！！！！", getAreaId());
        Map<String, guardreward> all = DataCenter.getAllData(guardreward.class);
        for (guardreward data : all.values()) {
            Collection<Currency> rewards = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());

            for (int i = data.getRankingmin() - 1; i < data.getRankingmax() && i < rankTop.size(); i++) {
                int rank = i + 1;
                RankEntry entry = rankTop.get(i);
                String title = GameString.守卫洛阳外城守卫伤害排名奖励_title(rank).getText();
                String content = GameString.守卫洛阳外城守卫伤害排名奖励_msg(rank).getText();

                switch (guardIndex) {
                    case 1:
                        title = GameString.守卫洛阳内城守卫伤害排名奖励_title(rank).getText();
                        content = GameString.守卫洛阳内城伤害排名奖励_msg(rank).getText();
                        break;
                    case 2:
                        title = GameString.守卫洛阳皇城守卫伤害排名奖励_title(rank).getText();
                        content = GameString.守卫洛阳皇城守卫伤害排名奖励_msg(rank).getText();
                        break;
                }

                new MailHandle().sendMail(entry.getKey(), MailTypeEnum.Mail, title, content, CCReason.洛阳守卫, rewards);
            }
        }
        logger.info("服务器%s::洛阳守卫奖励发完了", getAreaId());
    }

}
